function load_add_quiz()
{
	$.post("quiz/add_quiz.php",function(data){
								   $('#entire').html(data);
								   });
}
function mkread(id)
{
	$.post('inbox/mkread.php',{id:id},function(){
										$.post('inbox/show_inbox_admin.php',function(data){$('#entire').html(data);});
										 });
}
function load_quiz()
{
	$.post("quiz/quiz.php",function(data){
								   $('#entire').html(data);
								   });
}
function attendQuiz()
{
	$.post("quiz/attend_quiz.php",function(data){$('#entire').html(data);});
}
function add_quiz(){
	$('#add_quiz_status').hide();
	if(checkFields()==false)return false;
	updateAddQuiz();
}
function load_quiz_result()
{
	$.post("quiz/quiz_results.php",function(data){
								   $('#InnerDarkbox').html(data);
								   });
}
function updateAddQuiz()
{
	var dur="Nill";
	var num=$('#no_of_ques').val();
	var rev=0;
	if($('#limit').is(':checked'))
	{
		dur=$('#hour').val()+':'+$('#minutes').val()+':'+$('#seconds').val();
	}
	if($('#no_of_ques').val()==0)
	{
		num=$('#numbers').val();
	}
	if($('#review_answers').is(':checked'))
	{
		rev=1;
	}
	$.post("quiz/update_add_quiz.php",{quiz_type:$('#select_quiz_type').val(),quiz_name:$('#quiz_name').val(),no_of_ques:num,year_id:$('#quiz_for').val(),from_date:$('#start_date').val(),to_date:$('#end_date').val(),duration:dur,review:rev},function(data)																																																													 		{
   			if(data!="")
   			{
				$.post("quiz/add_qb.php",{type:$('#select_quiz_type').val(),testid:data},function(qb_data){
							$('#Innerheading').html('Add Questions ( '+$('#quiz_name').val()+' )');
							$('#InnerDarkbox').html(qb_data);
									});
   			}
		}
	);
}
function checkFields()
{
	if($('#quiz_name').val()=="")
	{
		$('#add_quiz_status').addClass('strykerError');
		$('#add_quiz_status').html('Quiz name Required').show();
		return false;
	}
	if($('#start_date').val()=="")
	{
		$('#add_quiz_status').addClass('strykerError');
		$('#add_quiz_status').html('Active from date Required').show();
		return false;
	}
	if($('#end_date').val()=="")
	{
		$('#add_quiz_status').addClass('strykerError');
		$('#add_quiz_status').html('Active till date Required').show();
		return false;
	}
	if($('#limit').is(':checked'))
	{
		if($('#hour').val()==0&&$('#minutes').val()==0&&$('#seconds').val()==0)
		{
			$('#add_quiz_status').addClass('strykerError');
			$('#add_quiz_status').html('Please Specify Time limit').show();
			return false;
		}
	}
	return true;
}
function add_time_limit()
{
	$('#time_limit').show();
	$('#time_limit_msg').show();
}
function remove_time_limit()
{
	$('#time_limit').hide();
	$('#time_limit_msg').hide();
}
function edit_update_qb()
{
	if(checkQB()==false)return false;
	var upload="";
	for(var i=1;i<=$('#no_q').val();i++)
	{
			if($('#type').val()==1){
			upload +=$('#question'+i).val()+":,&,:"+$('#select_answer'+i).val()+"::,::";}
			else if($('#type').val()==2){
upload +=$('#question'+i).val()+":,&,:"+$('#optone'+i).val()+":,&,:"+$('#opttwo'+i).val()+":,&,:"+$('#optthree'+i).val()+":,&,:"+$('#optfour'+i).val()+":,&,:"+$('#select_answer'+i).val()+"::,::";}
			else if($('#type').val()==3){
			upload +=$('#question'+i).val()+"::,::";}
	}
	upload = upload.slice(0, -5);
	$.post("quiz/update_edit_add_qb.php",{up:upload,type:$('#type').val(),testid:$('#testid').val(),no:$('#no_q').val()},function(data){																									
			load_quiz();
			$('#notice').html('Your Answers updated Successfully').show().fadeOut(5000);
			 });
}
function update_qb()
{
	if(checkQB()==false)return false;
	var upload="";
	for(var i=1;i<=$('#no_q').val();i++)
	{
			if($('#type').val()==1){
			upload +=$('#question'+i).val()+":,&,:"+$('#select_answer'+i).val()+"::,::";}
			else if($('#type').val()==2){
upload +=$('#question'+i).val()+":,&,:"+$('#optone'+i).val()+":,&,:"+$('#opttwo'+i).val()+":,&,:"+$('#optthree'+i).val()+":,&,:"+$('#optfour'+i).val()+":,&,:"+$('#select_answer'+i).val()+"::,::";}
			else if($('#type').val()==3){
			upload +=$('#question'+i).val()+"::,::";}
	}
	upload = upload.slice(0, -5);
	$.post("quiz/update_add_qb.php",{up:upload,type:$('#type').val(),testid:$('#testid').val(),no:$('#no_q').val()},function(data){																									
			load_quiz();
			$('#notice').html('Your Answers updated Successfully').show().fadeOut(5000);
			 });
}
function checkQB()
{
	$('#add_qb_status').hide();
	for(var i=1;i<=$('#no_q').val();i++)
	{
		$('#question'+i).removeClass('ErrorBox');
		if($('#question'+i).val()=="")
		{
			$('#question'+i).addClass('ErrorBox');
			$('#add_qb_status').html('Please add content to Question '+i).show().fadeOut(7000);
			scrollTop();
				return false;
		}
		if($('#type').val()==2)
		{
			$('#optone'+i).removeClass('ErrorBox');
			$('#opttwo'+i).removeClass('ErrorBox');
			$('#optthree'+i).removeClass('ErrorBox');
			$('#optfour'+i).removeClass('ErrorBox');
			if($('#optone'+i).val()=="")
			{
	$('#optone'+i).addClass('ErrorBox');
	$('#add_qb_status').html('Please add content to Option A in '+'Question :'+i).show().fadeOut(7000);
	scrollTop();
				return false;
			}
			if($('#opttwo'+i).val()=="")
			{
	$('#opttwo'+i).addClass('ErrorBox');
	$('#add_qb_status').html('Please add content to Option B in '+'Question :'+i).show().fadeOut(7000);
	scrollTop();
				return false;
			}
			if($('#optthree'+i).val()=="")
			{
	$('#optthree'+i).addClass('ErrorBox');
	$('#add_qb_status').html('Please add content to Option C in '+'Question :'+i).show().fadeOut(7000);
	scrollTop();
				return false;
			}
			if($('#optfour'+i).val()=="")
			{
	$('#optfour'+i).addClass('ErrorBox');
	$('#add_qb_status').html('Please add content to Option D in '+'Question :'+i).show().fadeOut(7000);
	scrollTop();
				return false;
			}
		}
	}
	return true;
}
function scrollTop(){
	$('html, body').animate({scrollTop:0}, 'slow');
}
function cloneInput(table)
{
	var no=$('#no_q').val();
	var $div = $(table).find('div:last').clone();
	$div.find('span').attr('id', function(){
          	var parts = this.id.match(/(\D+)(\d+)$/);
          	return parts[1] + ++parts[2];
     });
	$div.find('textarea').attr('id', function(){
          	var parts = this.id.match(/(\D+)(\d+)$/);
          	return parts[1] + ++parts[2];
     });
	 $div.find('textarea').attr('name', function(){
          	var parts = this.name.match(/(\D+)(\d+)$/);
          	return parts[1] + ++parts[2];
     });
	$div.find('input').attr('id', function(){
          	var parts = this.id.match(/(\D+)(\d+)$/);
          	return parts[1] + ++parts[2];
     });
	 		$div.find('input').attr('name', function(){
          	var parts = this.name.match(/(\D+)(\d+)$/);
          	return parts[1] + ++parts[2];
     });
		$div.find('select').attr('id', function(){
          	var parts = this.id.match(/(\D+)(\d+)$/);
          	return parts[1] + ++parts[2];
     });
	 		$div.find('select').attr('name', function(){
          	var parts = this.name.match(/(\D+)(\d+)$/);
          	return parts[1] + ++parts[2];
     });
	 $(table).append($div);
	 $('#no_q').val(parseInt(no)+1);
	 $('#question'+$('#no_q').val()).removeClass('ErrorBox');
	 $('#question'+$('#no_q').val()).val('');
	 $('#optone'+$('#no_q').val()).val('');
	 $('#optone'+$('#no_q').val()).removeClass('ErrorBox');
	 $('#opttwo'+$('#no_q').val()).val('');
	 $('#opttwo'+$('#no_q').val()).removeClass('ErrorBox');
	 $('#optthree'+$('#no_q').val()).val('');
	 $('#optthree'+$('#no_q').val()).removeClass('ErrorBox');
	 $('#optfour'+$('#no_q').val()).val('')
	 $('#optfour'+$('#no_q').val()).removeClass('ErrorBox');
	 $('#ques_up'+$('#no_q').val()).html($('#no_q').val());
 return true; 
}
function removeInput(table)
{
	if($('#no_q').val()!=1)
	{
		var no=$('#no_q').val();
		$(table+' div:last').remove();
		$('#no_q').val(parseInt(no)-1);
	}
	return false;
}
function display_timer(quiz_timer)
{
	window.quiz_timer=0;
	window.quiz_timer = parseFloat(quiz_timer);
	var end = 0; 
	var refresh=1000;
	if(window.quiz_timer >= end )
	{
		mytime=setTimeout('repeat_untill()',refresh);
	}
	else 
	{
		verifyAnswers();
	}
}
function repeat_untill() 
{
	var hours = Math.floor(window.quiz_timer /3600);
	var minutes = Math.floor((window.quiz_timer - (hours *3600 ))/60);
	var secs = Math.floor((window.quiz_timer - (hours *3600 ) - (minutes*60)));
	var time =    "Time left : " + hours + " Hours " + minutes + " Minutes " + secs + " Seconds " ;
	$('#timer').html(time);
	window.quiz_timer= window.quiz_timer- 1;
	tt=display_timer(window.quiz_timer);
}
function updateAnswers()
{
	window.quiz_timer=0;
}
function verifyAnswers()
{
	var qs=$('#post_data').val();
	qs=qs.slice(0,-1);
	var te=qs.split(':');
	var ans='';
	for(var i=0;i<te.length;i++)
	{
		if($('#q_type').val()==1)
		{
			if($('#'+te[i]+'_1').is(':checked'))
			{
				ans+='1'+':()%#@';
			}
			else if($('#'+te[i]+'_2').is(':checked'))
			{
				ans+='2'+':()%#@';
			}
			else
			{
				ans+='0'+':()%#@';
			}
		}
		else if($('#q_type').val()==2)
		{
			if($('#'+te[i]+'_1').is(':checked'))
			{
				ans+='1'+':()%#@';
			}
			else if($('#'+te[i]+'_2').is(':checked'))
			{
				ans+='2'+':()%#@';
			}
			else if($('#'+te[i]+'_3').is(':checked'))
			{
				ans+='3'+':()%#@';
			}
			else if($('#'+te[i]+'_4').is(':checked'))
			{
				ans+='4'+':()%#@';
			}
			else
			{
				ans+='0'+':()%#@';
			}
		}
		else if($('#q_type').val()==3)
		{
			ans+=($('#'+te[i]).val())+':()%#@';
		}
	}
	ans=ans.slice(0,-6);
	$.post("quiz/update_attend_quiz.php",{answer:ans,ques:qs,id:$('#q_id').val(),len:te.length},function(){
				$('#notice').html('Your Answers updated Successfully').show().fadeOut(5000);
						attendQuiz();
												});
}
function upload_question(id,q_type,name)
{
	$.post("quiz/add_qb.php",{type:q_type,testid:id},function(qb_data){
							$('#Innerheading').html('Add Questions ( '+name+' )');
							$('#InnerDarkbox').html(qb_data);
									});
}
function deleteQuiz(id)
{
	var ans; 
 	ans=window.confirm('Are you sure you delete?');
  	if (ans==true)
	{
		$.post("quiz/update_delete_quiz.php",{q_id:id},function(){
				$('#notice').html('Quiz removed Successfully').show().fadeOut(4000);
				load_quiz();
					});
	}
}
function edit_quiz(id)
{
	$.post("quiz/edit_add_quiz.php",{q_id:id},function(data){
								   $('#entire').html(data);
								   });
}
function update_edit_quiz(q_id)
{
	var dur="Nill";
	var num=$('#no_of_ques').val();
	var rev=0;
	if($('#limit').is(':checked'))
	{
		dur=$('#hour').val()+':'+$('#minutes').val()+':'+$('#seconds').val();
	}
	if($('#no_of_ques').val()==0)
	{
		num=$('#numbers').val();
	}
	if($('#review_answers').is(':checked'))
	{
		rev=1;
	}
	$.post("quiz/update_add_edit_quiz.php",{quiz_type:$('#select_quiz_type').val(),quiz_name:$('#quiz_name').val(),no_of_ques:num,year_id:$('#quiz_for').val(),from_date:$('#start_date').val(),to_date:$('#end_date').val(),duration:dur,review:rev,qu_id:q_id},function(data)																																																										 		{
			$.post("quiz/edit_add_qb.php",{type:$('#select_quiz_type').val(),testid:q_id},function(qb_data){
			$('#Innerheading').html('Add Questions ( '+$('#quiz_name').val()+' )');
			$('#InnerDarkbox').html(qb_data);
									});
		}
	);
}
function getResults(id)
{
	$.post("quiz/getresults.php",{test_id:id},function(data){
						$('#Innerheading').html('Quiz Results');
						$('#InnerDarkbox').html(data);
						  });
}
function endResult(id)
{
	$.post("quiz/endresult.php",{},function(data){
						$('#Innerheading').html('Not Attended');
						$('#InnerDarkbox').html(data);
											});
}
/*$.post("../plugins/request_success.php",{message:"Quiz added Successfully"},function(suc_data)
				{
					$('#InnerDarkbox').html(suc_data);
				}
				);
				$('#Innerheading').html('Thankyou');*/